<template>
	<view class="container">
		<view class="addres-list">
			<view class="address-item" v-for="(item, index) in list" :key="index">
				<view class="flex-y-center top" @tap="check(item)">
					<image src="/static/images/dHbuD6G9KcSLaJUC.png" class="flex-grow-0 img"></image>
					<view class="flex-grow-1">
						<view class="contacts">
							<text class="name">{{ item.name }}</text>
							<text class="phone">{{ item.mobile }}</text>
						</view>
						<view class="address">
							<text class="region">{{ item.province }} {{item.city}} {{item.district}}</text>
							<text class="detail">{{ item.addr }} {{item.info}}</text>
						</view>
					</view>
				</view>
				<view class="line"></view>
				<view class="item-option">
					<view class="_left">
						<label class="item-radio flex-y-center" v-if="item.is_default == 1">
							<image style="width: 40rpx;height: 40rpx;margin-right: 12rpx" src="/static/images/icon-checked.png" />
							<text class="text">默认</text>
						</label>
						<label class="item-radio  flex-y-center" @click.stop="handleSetDefault(item.id)" v-else>
							<image style="width: 40rpx;height: 40rpx;margin-right: 12rpx" src="/static/images/icon-uncheck.png" />
							<text class="text">选择</text>
						</label>
					</view>
					<view class="_right">
						<view class="events">
							<view class="event-item flex-y-center" @click="handleUpdate(item.id)">
								<image src="/static/images/bianji.png" class="icon"></image>
								<text class="title">编辑</text>
							</view>
							<view class="event-item flex-y-center" @click="handleRemove(item.id)">
								<image src="/static/images/shanchu.png" class="icon"></image>
								<text class="title">删除</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<empty v-if="!list.length"  tips="暂时还没有地址,快去添加吧~" />
		<!-- 底部操作按钮 -->
		<view class="footer-fixed">
			<view class="btn-wrapper">
				<view class="btn-item btn-item-main" @click="handleCreate()">添加地址</view>
			</view>
		</view>
	</view>
</template>
<script>
	import Empty from '../../components/empty'
	import { My } from '../../../apirequest/my-model.js';
	var m = new My();
	export default {
		components: {
			Empty
		},
		data() {
			return {
				// 收货地址列表
				list: [],
				// 默认收货地址
				defaultId: 1,
				options:{}
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			// 当前页面参数
			this.options = options
			
		},
		
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			this._getList()
		},

		methods: {
			_getList(){
				var that = this;
				m.get_addr_list((res) => {
					if(res.code == 1){
						that.list = res.data
					}
				})
			},
			/**
			 * 添加新地址
			 */
			handleCreate() {
				uni.navigateTo({
					url:'/pages2/shop/add-address/add-address'
				})
			},

			/**
			 * 编辑地址

			 */
			handleUpdate(addressId) {
				uni.navigateTo({
					url:'/pages2/shop/add-address/add-address?id='+addressId
				})
			},

			/**
			 * 删除收货地址
		
			 */
			handleRemove(addressId) {
				var that = this;
				uni.showModal({
					title: "提示",
					content: "您确定要删除当前地址吗?",
						success: function (res) {
							if (res.confirm) {
								m.addr_del({id:addressId},(res) => {
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
								if(res.code == 1){
									setTimeout(() => {
										that._getList()
									},1000)
								}
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
				});
			},


			/**
			 * 设置为默认地址
			 */
			handleSetDefault(addressId) {
				var that = this;
				m.set_addr_default({id:addressId},(res) => {
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					if(res.code == 1){
						setTimeout(() => {
							that._getList()
						},1000)
					}
				})

			},
			//选中
			check(addr){
				 var pages = getCurrentPages()
				 var beforePage = pages[pages.length - 2];//上个页面
				 beforePage.$vm.address = addr;
				 uni.navigateBack({
				 	delta:1
				 })
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background: #eee;
		font-size: 0.85rem;
	}
	.addres-list {
		padding-bottom: 120rpx;
	}

	// 项目内容
	.address-item {
		margin: 20rpx auto 20rpx auto;
		padding: 30rpx 3%;
		width: 91%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		border-radius: 16rpx;
		background: #fff;
	}

	.address-item .top .img {
		width: 70upx;
		height: 70upx;
		margin-right: 15upx;
	}

	.contacts {
		font-size: 30rpx;

		.name {
			margin-right: 16rpx;
		}

		.phone {
			font-size: 24rpx;
			color: #c7c7c7;
		}
	}

	.address {
		font-size: 28rpx;
		margin-top: 15rpx;

		.region {
			margin-right: 10rpx;
		}
	}

	.line {
		margin: 20rpx 0;
		border-bottom: 1rpx solid #f3f3f3;
	}

	.item-option {
		display: flex;
		justify-content: space-between;
		height: 48rpx;

		// 单选框
		.item-radio {
			font-size: 28rpx;

			.radio {
				vertical-align: middle;
				transform: scale(0.76)
			}

			.text {
				vertical-align: middle;
			}
		}

		// 操作
		.events {
			display: flex;
			align-items: center;
			line-height: 48rpx;

			.icon {
				width: 40upx;
				height: 40upx;
			}

			.event-item {
				font-size: 28rpx;
				margin-right: 22rpx;
				color: #4c4c4c;

				&:last-child {
					margin-right: 0;
				}

				.title {
					margin-left: 8rpx;
				}
			}
		}

	}


	// 底部操作栏
	.footer-fixed {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		z-index: 11;

		.btn-wrapper {
			height: 100%;
			display: flex;
			align-items: center;
			padding: 0 80rpx 40rpx;
		}

		.btn-item {
			flex: 1;
			font-size: 30rpx;
			height: 90rpx;
			line-height: 90rpx;
			text-align: center;
			color: #fff;
			border-radius: 10rpx;
		}

		.btn-item-main {
			background: #FF4544;
		}

	}
</style>
